package com.yeyks.app.function.rocketmq.consumer;

import cn.hutool.json.JSONUtil;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.yeyks.common.constants.rocketmq.TopicAndTag;
import com.yeyks.common.dto.mq.GrabTheOrderBToCMessage;
import com.yeyks.common.utils.ValidatorUtil;
import com.yeyks.commonReference.service.hotel.HotelSearchC2bService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 服务器反馈的抢单监听
 */
@Component
@Slf4j
public class GrabTheOrderSearchMessageListener implements MessageListener {

    @Autowired
    private HotelSearchC2bService hotelSearchC2bService;

    @Override
    public Action consume(Message message, ConsumeContext context) {
        String body = null;
        try {
            body = new String(message.getBody(), "utf-8");
            log.info("rocket:c2b抢单结果监听到:"+TopicAndTag.TOPIC_YYKS_NORMAL + TopicAndTag.TAG_WEB_TO_C_TAG +"监听服务端推送消息如下:message:{}", body);
            GrabTheOrderBToCMessage grabTheOrderBToCMessage = JSONUtil.toBean(body, GrabTheOrderBToCMessage.class);
            ValidatorUtil.validateEntity(grabTheOrderBToCMessage);
            hotelSearchC2bService.sendGrabTheOrderResult(grabTheOrderBToCMessage);
            return Action.CommitMessage;
        } catch (Exception e) {
            log.error("rocket:c2b抢单监听服务端推送消息消费失败:message:{}", body);
            e.printStackTrace();
            return Action.ReconsumeLater;
        }
    }

}
